package com.appspot.dcexplorer.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.RootPanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class dcExplorer implements EntryPoint
{
   private VerticalPanel mainPanel = new VerticalPanel();
   private FlexTable accountsTable = new FlexTable();
   private HorizontalPanel addPanel = new HorizontalPanel();
   private TextBox newAccountTextBox = new TextBox();
   private Button addAccountButton = new Button("Add");

   /**
    * This is the entry point method.
    */
   public void onModuleLoad() 
   {
      accountsTable.setText(0, 0, "Name");
      accountsTable.setText(0, 1, "Active");
      accountsTable.setText(0, 2, "Change");
      accountsTable.setText(0, 3, "Remove");

      // Assemble Add Account panel.
      addPanel.add(newAccountTextBox);
      addPanel.add(addAccountButton);

      // Assemble Main panel.
      mainPanel.add(accountsTable);
      mainPanel.add(addPanel);

      // Associate the Main panel with the HTML host page.
      RootPanel.get("mainPanel").add(mainPanel);

      // Move cursor focus to the input box
      newAccountTextBox.setFocus(true);

      // Listen for mouse events on the Add button.
      addAccountButton.addClickHandler(
         new ClickHandler() 
         {
            public void onClick(ClickEvent event) 
            {
               addAccount();
            }
         });
      // Listen for keyboard events in the input box.
      newAccountTextBox.addKeyPressHandler(
         new KeyPressHandler()
         {
            public void onKeyPress(KeyPressEvent event)
            {
               if (event.getCharCode() == KeyCodes.KEY_ENTER)
               {
                  addAccount();
               }
            }
         });
   }

   /**
    * Add account to FlexTable. Executed when the user clicks the addAccountButton or
    * presses enter in the newAccountTextBox.
    */
   private void addAccount()
   {
      final String symbol = newAccountTextBox.getText().trim();
      newAccountTextBox.setFocus(true);

      // Account name must be between 1 and 32 chars that are numbers, letters, spaces, underscores or dots.
      if (!symbol.matches("^[0-9a-zA-Z _\\.]{1,32}$"))
      {
         Window.alert("'" + symbol + "' is not a valid symbol.");
         newAccountTextBox.selectAll();
         return;
      }

      newAccountTextBox.setText("");

      // TODO Don't add the account if it's already in the table.

      // TODO Ask the server to add the account.

      // TODO Add the account returned by server to the table.

      // TODO Add a button to remove this account from the table.

   }
}
